package com.itheima.demo5genericity;

import java.beans.Beans;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;

/**
 * @author understand
 * @description:
 * @create 2025/5/18 15:10
 */
public class BeanConvertUtils extends BeanUtils {
    public static <S,T> T convert(S source, Supplier<T> targetSupplier) {
      if (source == null|| targetSupplier == null)
          return null;
      T target = targetSupplier.get();
      BeanUtils.copyProperties(source, target);
      return target;
    }
    public static <S,T> List<T> convert(List<S> sources, Supplier<T> targetSuppliers,ConvertCallBack<S, T> callBack) {
        if (sources == null || targetSuppliers == null)
            return null;
        List<T> targets = new ArrayList<>();
        for (S source : sources) {
            T target = targetSuppliers.get();
            BeanUtils.copyProperties(source, target);
            if (callBack != null) {
                callBack.callBack(source, target);
            }
            targets.add(target);
        }
        return targets;
    }
    /**
     * 回调接口
     *
     * @param <S> 源对象类型
     * @param <T> 目标对象类型
     */
    @FunctionalInterface
    public interface ConvertCallBack<S, T> {
        /**
         * 在赋值给目标对象后，再对目标对象或者源对象做的操作
         * @param s 源对象
         * @param t 目标对象
         */
        void callBack(S s, T t);
    }
}
